@echo off
set OLDDIR=%CD%
cd ..
set SVCDIR=%CD%
cd %OLDDIR%

echo ==== Removing Source and Classes Directories =====
rd /q /s bin\ > nul 2> nul
rd /q /s src\ > nul 2> nul

echo ============= Starting Service ===================
md bin
md src
echo Starting Service from:
echo %SVCDIR%\SmartDatingService
start "" "C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exe" /port:61673 /path:"%SVCDIR%\SmartDatingService"

:waitForServerStart
del /q query.txt > nul 2> nul
netstat -ao -n -p TCP | find "127.0.0.1:61673" | find "LISTEN" > query.txt
for %%R in (query.txt) do if %%~zR equ 0 goto waitForServerStart

del /q query.txt > nul 2> nul
echo Service Started

echo ==== Generating Java Sources and Class files =====
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_14
"C:\Program Files\NetBeans 6.9.1\mobility\Java_ME_platform_SDK_3.0\bin\wscompile.exe" -gen -d bin -s src -cldc1.1 config.xml
pause

echo =============== Terminating Service ==============
taskkill /F /IM WebDev.WebServer40.exe

echo ====================== DONE! =====================